/*
 * @Description: user 路由模块
 * @Author: luozhao
 * @Date: 2021-01-19 17:34:53
 */
var express = require('express');
var expressWs = require('express-ws');

var router = express.Router();
expressWs(router);

router
  .ws('/login', function (ws, req) {
    ws.on('message', function (msg) {
      // 业务代码
      const name = JSON.parse(msg).name;
      const resp = {
        token: req.query.token,
        name: name,
        message: `${name} login`
      };
      console.log('login msg req: ', resp);
      ws.send(JSON.stringify(resp));
    });
  })
  .get('/login', function (req, res) {
    console.log("🚀 ~ file: user.router.js ~ line 21 ~ req", req.query)
    res.send(req.query);
  })
  .post('/login', function (req, res) {
  });

router
  .ws('/talking/:usessionId', function (ws, req) {
    console.log("🚀 ~ file: user.router.js ~ line 34 ~ req", req.query)
    console.log("🚀 ~ file: user.router.js ~ line 34 ~ req", req.params);
    ws.on('message', function (msg) {
      // 业务代码
      const { context = '', name = '' } = JSON.parse(msg);
      const resp = {
        name: name,
        message: `${name} talking ${context}`
      };
      console.log('talking msg req: ', resp);
      ws.send(JSON.stringify(resp));
    });
  })

module.exports = router;